package edu.ccut.saturn.component.loader.node;

/**
 * 用于解析action组件配置文件中“视图”结点的加载类。
 * <p>继承了<code>NodeInfoLoader</code>,并实现了<code>loadNode</code>方法。<p>
 * 
 * @author 谷钰
 * @version 1.0 2008.8.18
 * 
 * @see <code>ViewNodeInfo</code>
 */
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;
import org.osgi.framework.Bundle;

import edu.ccut.saturn.component.INodeInfo;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.impl.node.ViewNodeInfo;

public class ViewNodeInfoLoader extends NodeInfoLoader {

	public static final String KEY_NODETYPE_VIEW = "view";

	public static final String KEY_NODE_LOCATION = "location";

	public static final String KEY_NODE_TYPE = "type";

	@SuppressWarnings("unchecked")
	public List<INodeInfo> loadNode(Element root, Bundle bundle)
			throws SaturnComponentException {

		List<Element> viewNodeElements = root.elements(KEY_NODETYPE_VIEW);
		List<INodeInfo> viewNodeInfos = new ArrayList<INodeInfo>(
				viewNodeElements.size());

		for (Element viewNodeElement : viewNodeElements) {
			String id = viewNodeElement.attributeValue(KEY_NODE_ID).trim();
			String x = viewNodeElement.attributeValue(KEY_NODE_X).trim();
			String y = viewNodeElement.attributeValue(KEY_NODE_Y).trim();
			String name = viewNodeElement.elementTextTrim(KEY_NODE_NAME);
			String description = viewNodeElement
					.elementTextTrim(KEY_NODE_DESCRIPTION);

			String location = viewNodeElement
					.elementTextTrim(KEY_NODE_LOCATION);

			String type = viewNodeElement.elementTextTrim(KEY_NODE_TYPE);

			logger.debug("  load view component id = \"" + id + "\"" +
					"name = \"" + name + "\" description = \"" + description
					+ "\" location = \"" + location + "\" type = \"" + type + "\"");

			ViewNodeInfo viewNodeInfo = new ViewNodeInfo(id, name, description,
					location, type, x, y);

			viewNodeInfos.add(viewNodeInfo);
		}

		return viewNodeInfos;
	}

}
